package com.dq.service;


import com.dq.entity.QQUser;
import com.dq.entity.QQUserLogin;
import com.dq.mapper.QQFriendMapper;
import com.dq.mapper.QQLoginMapper;
import com.dq.mapper.QQUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

/**
 * @Author: Dang Qi
 * @Date: 2021/5/9 10:35
 * @Description:
 */
@Service
public class UserService {

    @Autowired
    QQLoginMapper loginMapper;

    @Autowired
    QQUserMapper userMapper;

    @Autowired
    QQFriendMapper friendMapper;

    @Transactional
    public boolean addQqUser(QQUser user) {
        QQUserLogin login = QQUserLogin.builder()
                .qqNumber(user.getQqNumber())
                .passwd(user.getPasswd())
                .secret(Base64.getEncoder().encodeToString(user.getPasswd().getBytes(StandardCharsets.UTF_8)))
                .build();
        return userMapper.insert(user)!=0 && loginMapper.insert(login)!=0;
    }

    public boolean queryQqExist(String qqNumber) {
        return userMapper.selectByNo(qqNumber) != null;
    }
    public List<String> getQqFriend(String qqNumber) {
        return friendMapper.getFriend(qqNumber);

    }
    public boolean queryLoginCheck(String qqNumber, String passwd) {
        String secretByNo = loginMapper.getSecretByNo(qqNumber);
        if(secretByNo==null) return false;
        return Base64.getEncoder().encodeToString(passwd.getBytes(StandardCharsets.UTF_8)).equals(secretByNo);
    }

}
